<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * 获取今天的开始和结束时间
 * @return mixed
 */
function getToDayTime()
{
    $str = date("Y-m-d", time()) . " 0:0:0";
    $data["star"] = strtotime($str);
    $str = date("Y-m-d", time()) . " 24:00:00";
    $data["end"] = strtotime($str);
    return $data;
}

function getHeader($name, $default = null)
{

return isset($_SERVER['HTTP_' . strtoupper(str_replace('-', '_', $name))]) ? $_SERVER['HTTP_' . strtoupper(str_replace('-', '_', $name))]:  $default;
}

function get_http_type()
{
    $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
    return $http_type;
}

function genUserName()
{
    $microtime = substr(microtime(true), strpos(microtime(true), ".") + 1);
    $chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $username = "";
    for ($i = 0; $i < 6; $i++) {
        $username .= $chars[mt_rand(0, strlen($chars))];
    }
 
    return $microtime . strtoupper(base_convert(time() - 1420070400, 10, 36)) . $username;
}

/**
 * @param $url
 * @param array $header
 * @return bool|string
 */
function curl_get($url, $header = array())
{

    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 超时设置,以秒为单位
    curl_setopt($curl, CURLOPT_TIMEOUT, 1);

    // 超时设置，以毫秒为单位
    // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);

    // 设置请求头
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    //执行命令
    $data = curl_exec($curl);
    return $data;
    // if (false == $data) {
    //     $res['msg'] = curl_error($curl);
    // } else {
    //     $ret = json_decode($ret, true);
    //     if ($ret["code"] === 0) {
    //         return $ret;
    //     } else {
    //         $res = ['code' => $ret["code"], 'msg' => $ret["msg"], 'data' => new \stdClass()];
    //     }
    // }
    // return $res;
}